package com.bootdo.system.test

import com.bootdo.common.annotation.Log
import com.bootdo.common.utils.PageUtils
import com.bootdo.common.utils.Query
import com.bootdo.common.utils.R
import java.util.Date
import org.apache.ibatis.annotations.Mapper
import org.apache.shiro.authz.annotation.RequiresPermissions
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.stereotype.Service
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.*
import com.bootdo.system.dao.TestDao

/**
 * 
 *
 * @author jafar.tang
 * @date 2018-10-30 09:09:03
 */
//========== controller
@Controller
@RequestMapping("/system/test")
open class TestController {
  open var prefix = "system/test"

  @Autowired
  lateinit open var testService:TestService

  @GetMapping("")
  @RequiresPermissions("system:test:list")
  fun index(m:Model):String = "$prefix/list"

  @GetMapping("/list")
  @ResponseBody
  @RequiresPermissions("system:test:list")
  fun list(@RequestParam params:Map<String, Any>):PageUtils {
    val query = Query(params)
    val list = testService.list(query)
    val count = testService.count(query)
    return PageUtils(list, count)
  }

  @GetMapping("/add")
  @Log("添加")
  @RequiresPermissions("system:test:add")
  fun add(m:Model):String {
    m.addAttribute("pageMode", "save")
    m.addAttribute("excludeFields", "['id','createTime',]")
    m.addAttribute("entity", TestDO())
    return "$prefix/form"
  }

  @GetMapping("/edit/{id}")
  @Log("编辑")
  @RequiresPermissions("system:test:update")
  fun add(m:Model, @PathVariable id:Long):String {
    m.addAttribute("entity", testService.get(id))
    m.addAttribute("excludeFields", "['createTime',]")
    m.addAttribute("pageMode", "update")
    return "$prefix/form"
  }

  @GetMapping("/unique/{id}")
  @Log("查看")
  @RequiresPermissions("system:test:unique")
  fun unique(m:Model, @PathVariable id:Long):String {
    m.addAttribute("entity", testService.get(id))
    m.addAttribute("excludeFields", "['id']")
    m.addAttribute("pageMode", "unique")
    return "$prefix/form"
  }

  @PostMapping("/save")
  @Log("保存")
  @RequiresPermissions("system:test:add")
  @ResponseBody
  fun save(entity:TestDO):R? {
    if(testService.save(entity) > 0) {
      return R.ok().put("entity", entity)
    }
    return R.error()
  }

  @PostMapping("/update")
  @Log("修改")
  @RequiresPermissions("system:test:update")
  @ResponseBody
  fun add(entity:TestDO):R? {
    if(testService.update(entity) > 0) {
      return R.ok()
    }
    return R.error()
  }

  @PostMapping("/remove")
  @Log("删除")
  @RequiresPermissions("system:test:remove")
  @ResponseBody
  fun remove(id:Long):R {
    if(testService.remove(id) > 0) {
      return R.ok()
    }
    return R.error()
  }

  @PostMapping("/batchRemove")
  @Log("批量删除")
  @RequiresPermissions("system:test:remove")
  @ResponseBody
  fun batchRemove(ids:LongArray):R {
    if(testService.batchRemove(ids) > 0) {
      return R.ok()
    }
    return R.error()
  }
}


//=========== service
interface TestService {
  fun get(id: Long?): TestDO

  fun unique(map: Map<String, Any>): TestDO

  fun list(map: Map<String, Any>): List<TestDO>

  fun count(map: Map<String, Any>): Int

  fun save(entity: TestDO): Int

  fun update(entity: TestDO): Int

  fun remove(id: Long?): Int

  fun batchRemove(ids: LongArray): Int
}

@Service
class TestServiceImpl:TestService {
  @Autowired
  lateinit open var testDao:TestDao

  override fun get(id: Long?): TestDO = testDao.get(id)

  override fun unique(map: Map<String, Any>): TestDO = testDao.unique(map)

  override fun list(map: Map<String, Any>): List<TestDO> = testDao.list(map)

  override fun count(map: Map<String, Any>): Int = testDao.count(map)

  override fun save(entity: TestDO): Int {
    entity.createTime = Date()
    return testDao.save(entity)
  }

  override fun update(entity: TestDO): Int = testDao.update(entity)

  override fun remove(id: Long?): Int = testDao.remove(id)

  override fun batchRemove(ids: LongArray): Int = testDao.batchRemove(ids)
}

//=========== domain
data class TestDO(
    var id:Int? = null,
    var name:String? = null,
    var age:Int? = null,
    var createTime:Date? = null
)